import requests
import os
import pandas as pd
import time
import datetime
import json

def get_today():
    # 获取今天的字符串
    today = time.strftime("%Y-%m-%d", time.localtime(time.time()))
    print(today)
    print(type(today))
    return today

def getYesterday():
    today=datetime.date.today()
    oneday=datetime.timedelta(days=1)
    yesterday=today-oneday
    return yesterday

def get_tomorrow():
    today = datetime.date.today()
    tomorrow = today + datetime.timedelta(days=1)
    print("明天的日期:" + str(tomorrow))
    tomorrow_str = tomorrow.strftime("%Y-%m-%d")

    print(type(tomorrow_str))

    return tomorrow_str


def hotel_spider(cityId, keyword, today_str, tomorrow_str):
    # import requests

    cookies = {
        'GUID': '09031123317544854960',
        'nfes_isSupportWebP': '1',
        'nfes_isSupportWebP': '1',
        '_bfaStatusPVSend': '1',
        '_RSG': 'cBK1ZzXWNP7Hcqhnxg4sIA',
        '_RDG': '28191737ca97c028c334caf219e84887b6',
        '_RGUID': '7657821f-6305-42e6-a4b6-12aef1af4991',
        '_abtest_userid': 'f18de1c5-a97c-4962-b4f9-9d06ce830ecf',
        'cticket': 'F99FFBACF04466B8A1DD8E1D04802617385E7D9008057FE4DCF82DDEB9E1BE0E',
        'login_type': '0',
        'login_uid': '727EFF7A75E4D56FE6634D72893E8138',
        'DUID': 'u=B249C653B144B462954DA5E3DA22D672&v=0',
        'IsNonUser': 'F',
        'AHeadUserInfo': 'VipGrade=0&VipGradeName=%C6%D5%CD%A8%BB%E1%D4%B1&UserName=&NoReadMessageCount=0',
        'UUID': '47D4AA139CFD4359BF780CD6BCD8EFC9',
        'librauuid': '',
        'Union': 'OUID=&AllianceID=66672&SID=1693366&SourceID=&AppID=&OpenID=&exmktID=&createtime=1679327711&Expires=1679932510829',
        '_RF1': '173.82.245.30',
        'hotelhst': '1164390341',
        '_bfi': 'p1%3D212093%26p2%3D212093%26v1%3D17%26v2%3D14',
        '_pd': '%7B%22_o%22%3A2%2C%22s%22%3A260%2C%22_s%22%3A0%7D',
        '_bfaStatus': 'send',
        '_bfa': '1.1679325879255.3pb3y.1.1679327751039.1679331252753.4.19.212092',
        '_bfs': '1.8',
        '_ubtstatus': '%7B%22vid%22%3A%221679325879255.3pb3y%22%2C%22sid%22%3A4%2C%22pvid%22%3A19%2C%22pid%22%3A212093%7D',
    }

    headers = {
        'authority': 'm.ctrip.com',
        'accept': '*/*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'content-type': 'application/json',
        # 'cookie': 'GUID=09031123317544854960; nfes_isSupportWebP=1; nfes_isSupportWebP=1; _bfaStatusPVSend=1; _RSG=cBK1ZzXWNP7Hcqhnxg4sIA; _RDG=28191737ca97c028c334caf219e84887b6; _RGUID=7657821f-6305-42e6-a4b6-12aef1af4991; _abtest_userid=f18de1c5-a97c-4962-b4f9-9d06ce830ecf; cticket=F99FFBACF04466B8A1DD8E1D04802617385E7D9008057FE4DCF82DDEB9E1BE0E; login_type=0; login_uid=727EFF7A75E4D56FE6634D72893E8138; DUID=u=B249C653B144B462954DA5E3DA22D672&v=0; IsNonUser=F; AHeadUserInfo=VipGrade=0&VipGradeName=%C6%D5%CD%A8%BB%E1%D4%B1&UserName=&NoReadMessageCount=0; UUID=47D4AA139CFD4359BF780CD6BCD8EFC9; librauuid=; Union=OUID=&AllianceID=66672&SID=1693366&SourceID=&AppID=&OpenID=&exmktID=&createtime=1679327711&Expires=1679932510829; _RF1=173.82.245.30; hotelhst=1164390341; _bfi=p1%3D212093%26p2%3D212093%26v1%3D17%26v2%3D14; _pd=%7B%22_o%22%3A2%2C%22s%22%3A260%2C%22_s%22%3A0%7D; _bfaStatus=send; _bfa=1.1679325879255.3pb3y.1.1679327751039.1679331252753.4.19.212092; _bfs=1.8; _ubtstatus=%7B%22vid%22%3A%221679325879255.3pb3y%22%2C%22sid%22%3A4%2C%22pvid%22%3A19%2C%22pid%22%3A212093%7D',
        'cookieorigin': 'https://m.ctrip.com',
        'origin': 'https://m.ctrip.com',
        'referer': 'https://m.ctrip.com/webapp/hotels/list?city=2&atime=20230321&days=1&kf=hotkeyword-%E4%B8%9C%E6%96%B9%E6%98%8E%E7%8F%A0%7C%7C%7C%E4%B8%9C%E6%96%B9%E6%98%8E%E7%8F%A0%7C',
        'sec-ch-ua': '"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
        'x-ctrip-soa2-req-route': 'custom-h5',
    }

    params = {
        '_fxpcqlniredt': '09031123317544854960',
        'x-traceID': '09031123317544854960-1679331600114-5480416',
    }

    json_data = {
        'ssr': False,
        'cityId': cityId,
        'districtId': 0,
        'checkinDate': today_str,
        'checkoutDate': tomorrow_str,
        'pageIndex': 1,
        'pageSize': 10,
        'orderItem': '',
        'pageCode': '212093',
        'nearbySearch': 0,
        'hiddenHotelIds': None,
        'isHourRoomSearch': False,
        'preHotelIds': '',
        'preCount': 0,
        'isOrderByUser': False,
        'filterInfo': {
            'keyword': keyword,
            'lowestPrice': 0,
            'highestPrice': 0,
            'filterItemList': [],
            'starItemList': [],
            'orderItem': 'sort-0|1',
            'referencePoint': None,
        },
        'userCoordinate': {},
        'channel': 0,
        'sessionId': '',
        'hotelIdList': None,
        'mapType': 3,
        'poiValue': '',
        'geoValue': '',
        'session': {
            'key': '537c37709b3627ef3cc836Q3f06562bc7016f139e89917385d2d26ccc2007f40',
            'fp': 'fO6vM8v1PvGzWqnKsXeq1EO1WdmIpaRSFJnYUcjTHeZSeFpYd0E5zwL4joDrkYzQKZPykGKo8eaDEnpj5fWDPRDw8BJkY16J7lE4QwtkjQgwMGWOtjXJHZRTZwNLvQljsJGXeXZwmPvM7jnJFHv4dKn7YbfwOnjhaeMSiGLYDzrBJA8eBYcgRcwtlRgsynhj5TvgsEZqvqdW4kjU0JPFIXQjaYS4IFBEcDjG5Yo1iBXwatRfAEBhWXSvonxZ8YgYBNvMHv6Gyf0Wt1Yg9WPYX7ePhi5zJnvkninFIOYDDx49WMSwf5vS0Ys5y97jLsv8Ze1bYGmj90ygJOsv9fY5QypmjagvZkeBLYnzjkHy1JlMYFsv34WotWkDw9Uvf5j4Y56yHzRLDiBkvtGeSaYNbi8fY7Uw5JGMRdYh6KbBWNFxT6R3FEXvoY9kYPnW33xGPylXwTpimY81x1lJ4TwL9YlgiD3i7kiFqjAMRqOvUbKDYp0w3dwNawnOy6NwkMY49wOGyoswkzYUowLby3LR8Mv0YbUi5Ze4GW73Rdswg4yaSvzcipsio1R6cJHovQdv3ARMcvs1RMnJ7SeadRQYQyOyz4KgLxG4KD1WtYBlJ4hiOkELmRqswd0yzPv1oi5mYzgJgPjZLyUQvDoYQOJ67Jd3im7RAoY4YXMeU6IfGEMdj4Sw71vBbjNMwpEHzEkYg6RMnR17rn1RUbvMGYhoW18eTzRU9WBGjqSWPJGtjZUinYkoyGlwcTK7nRsqvNsYcaWmBeDtRlgW4kEznWsgE9my0fwXY7UjqFeadRt1R9Xw8lyPsvqlifsi3FRkLJ9qv7Uv9lRA9jUmvUaEFsJPhjmYXjc7izkrf1Eoljp4WQTWF5Wc7YAMYk3Y9lRXFYMlW7LY6XYqQYUfj14e7DE4QWlbeT9whle1qjqOYzhycaEAfjMbEcDra4jS9wqmyL7WMybTRGYUgRt1WAgWlXW6DWL3YQYGgx97WMMx4lvDMENHWcFyA0j8JFcvDAEU5WP5yh8jDmxDBJPQjmY1TYNpJUXEFZElPEaNEopRpqEOPIUBw96JqYp6JqUiLtv1kEamEOoENcYFqYAZY3bYokRz5vNM',
        },
        'head': {
            'cid': '09031123317544854960',
            'ctok': '',
            'cver': '1678961060571',
            'lang': '01',
            'sid': '1693366',
            'syscode': '09',
            'auth': '',
            'xsid': '',
            'extension': [
                {
                    'name': 'supportWebP',
                    'value': 'true',
                },
            ],
            'aid': '66672',
            'ouid': '',
            'vid': '1679325879255.3pb3y',
        },
    }

    try:
        response = requests.post(
            'https://m.ctrip.com/restapi/soa2/25850/getHotelList',
            params=params,
            cookies=cookies,
            headers=headers,
            json=json_data,
        )
    except:
        proxies = {
            "https": "127.0.0.1:7890"
        }
        response = requests.post(
            'https://m.ctrip.com/restapi/soa2/25850/getHotelList',
            params=params,
            cookies=cookies,
            headers=headers,
            json=json_data,
            proxies=proxies
        )
    html = response.json()
    print(html)
    return html

def main():
    r_csv_name = 'ultimate_sight.csv'
    r_csv_path = os.path.join(os.path.abspath(
        os.path.join(os.path.join(os.path.dirname(__file__), ".."), 'static/spider/data/ultimate_sight')),
        r_csv_name)
    ultimate_sight_datas = pd.read_csv(r_csv_path, encoding='utf-8').to_dict(orient='record')
    r_csv_name = 'ultimate_city.csv'
    r_csv_path = os.path.join(os.path.abspath(
        os.path.join(os.path.join(os.path.dirname(__file__), ".."), 'static/spider/data/ultimate_city')),
        r_csv_name)
    ultimate_city_dict = pd.read_csv(r_csv_path, encoding='utf-8').set_index('cname').to_dict()['cityId']
    # 合并两个 DataFrame
    for ultimate_sight_data in ultimate_sight_datas:
        poiName = ultimate_sight_data['poiName']
        cname = ultimate_sight_data['districtName']
        if cname in ultimate_city_dict:
            cityId = ultimate_city_dict[cname]
            ultimate_sight_data['cityId'] = cityId
            today_str = get_today()
            tomorrow_str = get_tomorrow()
            hotel_datas = hotel_spider(cityId, poiName, today_str, tomorrow_str)
            to_csv_data = {}
            to_csv_data['hotelName'] = []
            to_csv_data['hotelId'] = []
            to_csv_data['price'] = []
            to_csv_data['positionDesc'] = []
            for hotel_data in hotel_datas['hotelInfoList']:
                to_csv_data['hotelName'].append(hotel_data['hotelName'])
                to_csv_data['hotelId'].append(hotel_data['hotelId'])
                to_csv_data['price'].append(hotel_data['price'])
                to_csv_data['positionDesc'].append(hotel_data['positionDesc'])
            t_csv_name = '{}_{}_{}_hotel.csv'.format(cname, poiName, today_str)
            t_csv_path = os.path.join(os.path.abspath(
                os.path.join(os.path.join(os.path.dirname(__file__), ".."), 'static/spider/data/hotel')),
                t_csv_name)
            pd.DataFrame(to_csv_data).to_csv(t_csv_path, index=False, encoding='utf-8')
        time.sleep(5)

if __name__ == '__main__':
    main()